package com.shuoyue.adven.main.bussiness;

import com.shuoyue.adven.entity.Adv;

import java.util.Iterator;
import java.util.List;

public class MatchUtils {
    public static List<Adv> match(List<Adv> local, List<Adv> remote) {
        //将新的增加到列表
        for (Adv AdvRemote : remote) {
            boolean isFindSame = false;
            for (Adv AdvLocal : local) {
                if (AdvLocal.getId() == AdvRemote.getId()) {
                    isFindSame = true;
                    break;
                }
            }
            if (!isFindSame) {
                local.add(AdvRemote);
            }
        }

        //修改id已有的，删除服务器已经通知删除的
        Iterator<Adv> iterator = local.iterator();
        while (iterator.hasNext()) {
            Adv Adv = iterator.next();
            boolean isFind = false;
            for (Adv AdvRemote : remote) {
                if (AdvRemote.getId() == Adv.getId()) {
                    Adv.setFontContent(AdvRemote.getFontContent());
                    Adv.setLogo(AdvRemote.getLogo());
                    Adv.setImgShowMills(AdvRemote.getImgShowMills());
                    Adv.setPlayTimesCircle(AdvRemote.getPlayTimesCircle());
                    Adv.setPlayTimesTotal(AdvRemote.getPlayTimesTotal());
                    Adv.setTitle(AdvRemote.getTitle());
                    Adv.setType(AdvRemote.getType());
                    isFind = true;
                    break;
                }
            }
            if (!isFind) {
                iterator.remove();
            }
        }
        return local;
    }

    public static boolean isSame(List<Adv> list, List<Adv> list1) {
        long st = System.nanoTime();
        if (list.size() != list1.size()) {
            System.out.println("消耗时间：" + (System.nanoTime() - st));
            return false;
        }
        for (Adv adv : list) {
            if (!list1.contains(adv)) {
                System.out.println("消耗时间：" + (System.nanoTime() - st));
                return false;
            }
        }
        System.out.println("消耗时间：" + (System.nanoTime() - st));
        return true;
    }
}
